<template>
  <view class="collection-container">
    <!-- #ifndef MP-TOUTIAO -->
    <back-nav-bar title="我的收藏"></back-nav-bar>
    <!-- #endif -->

    <view v-if="collectionData.length > 0">
      <view v-for="item in collectionData" :key="item.id" class="item-card">
        <image
          :src="type === 1 ? item.smallPicurl : item.imgUrl"
          alt="图片"
          @click="type === 1 ? goPreview(item.id) : goPreviewAlbum(item.id)"
          class="item-image"
        />
        <view class="item-details">
          <text class="item-title">{{ item.description }}</text>
          <text class="item-score">{{ item.score !== undefined && item.score !== null ? `评分:${item.score}` : '暂无评分' }}</text>
        </view>
      </view>
    </view>
    <view v-else class="empty-state"> 暂无收藏内容 </view>
  </view>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { apiGetUserCollection } from '@/api/tsApis'
import { getUserId } from '@/utils/auth'
import { goPreview, goPreviewAlbum } from '@/utils/common'
import BackNavBar from '@/components/back-nav-bar/back-nav-bar.vue'

const collectionData = ref([])
const type = ref(1) // 默认类型为壁纸

// 获取收藏列表
const fetchCollections = () => {
  const params = {
    userId: getUserId(),
    type: type.value,
    page: 1,
    pageSize: 10000, // 一次性获取全部收藏内容
  }
  apiGetUserCollection(params).then((res) => {
    if (res.code === 200) {
      collectionData.value = res.data
    }
  })
}

onLoad(() => {
  fetchCollections()
})

// 切换类型的方法
const toggleType = () => {
  type.value = type.value === 1 ? 0 : 1
  fetchCollections()
}

// 返回上一页的方法
const goBack = () => {
  uni.navigateBack({
    delta: 1,
  })
}
</script>
<style scoped lang="scss">
.collection-container {
  position: relative; // 确保返回按钮相对定位
  padding: 20rpx;
  background-color: #f9f9f9;
  min-height: 100vh;

  .back-button {
    position: absolute;
    top: 20rpx;
    left: 20rpx;
    display: flex;
    align-items: center;
    cursor: pointer;
    transition: transform 0.3s ease;

    &:hover {
      transform: scale(1.1);
    }

    text {
      margin-left: 5rpx;
      font-size: 28rpx;
      color: #333;
    }
  }

  .header {
    font-size: 36rpx;
    font-weight: bold;
    color: #555;
    margin-bottom: 20rpx;
    text-align: center;
  }

  .toggle-button {
    margin: 10px auto;
    padding: 10px 20px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease;

    &:hover {
      background-color: #0056b3;
    }
  }

  .item-card {
    display: flex;
    background-color: #fff;
    padding: 20rpx;
    border-radius: 12rpx;
    box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    flex-wrap: nowrap; // 确保子元素在同一行内
    height: 300rpx; // 增加卡片的高度

    &:hover {
      transform: translateY(-5rpx);
      box-shadow: 0 6rpx 12rpx rgba(0, 0, 0, 0.2);
    }

    .item-image {
      width: 70%; // 调整图片宽度
      height: 250rpx; // 增加图片高度
      object-fit: cover;
      border-radius: 8rpx;
      margin-right: 10rpx; // 给右侧文本留出空间
    }

    .item-details {
      width: 30%; // 调整文本信息宽度
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding-left: 10rpx;

      .item-title {
        font-size: 28rpx;
        color: #333;
        margin-bottom: 5rpx;
      }

      .item-score {
        font-size: 24rpx;
        color: #666;
      }
    }
  }

  .empty-state {
    text-align: center;
    margin-top: 50rpx;
    color: #999;
    font-size: 28rpx;
  }
}
</style>
